﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class PrinterControl

    Public Structure PrinterConfig
        Dim ConName As String
        Dim ConType As String
        Dim ConEnable As Boolean
    End Structure

    Dim Printer As New PrinterConfig
    Dim PrinterSetting As String

    Public Sub SetPrinterConfig(ByVal Printer As PrinterConfig)
        PrinterList.SelectedValue = Printer.ConName
        PrinterType.SelectedValue = Printer.ConType
        PrinterEnable.IsChecked = Printer.ConEnable

    End Sub

    Public Function GetPrinterConfig(ByVal json As Boolean)
        Dim settings As String
        Printer.ConName = PrinterList.SelectedValue
        Printer.ConType = PrinterType.SelectedValue
        Printer.ConEnable = PrinterEnable.IsChecked

        If (json) Then
            settings = JsonConvert.SerializeObject(Printer)
            Return settings
        Else
            Return Printer
        End If
    End Function

    Public Sub LoadPrinterConfig(ByVal settings As String)
        Printer = ParsePrinterConfig(settings)
        Me.SetPrinterConfig(Printer)
    End Sub

    Public Shared Function ParsePrinterConfig(ByVal settings As String)
        Dim Printer As New PrinterControl.PrinterConfig
        Dim jres As JObject = CType(JsonConvert.DeserializeObject(settings), Linq.JObject)

        If jres IsNot Nothing Then
            Printer = jres.ToObject(Of PrinterControl.PrinterConfig)()
        End If
        Return Printer
    End Function

End Class
